package org.danger.dy.common.web;

import org.danger.dy.common.web.logging.DyLoggingFilter;
import org.danger.dy.common.web.logging.DyRepeatableFilter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
@AutoConfigureBefore({WebMvcAutoConfiguration.class})
public class DyWebAutoConfiguration {

    @Bean(name = "loggingFilter")
    public FilterRegistrationBean<DyLoggingFilter> loggingFilterRegisterBean() {
        FilterRegistrationBean<DyLoggingFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new DyLoggingFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("loggingFilter");
        filterRegistrationBean.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE + 1); //order的数值越小 则优先级越高
        return filterRegistrationBean;
    }

    @Bean(name = "repeatableFilter")
    public FilterRegistrationBean<DyRepeatableFilter> repeatableFilter() {
        FilterRegistrationBean<DyRepeatableFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new DyRepeatableFilter());
        registration.addUrlPatterns("/*");
        registration.setName("repeatableFilter");
        registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
        return registration;
    }
}
